// import { login, logout, getMenuList } from 'api/common'
import { login, logout, getUserInfo } from 'api/common'
import utils from 'utils'
import { resetRouter } from '@/routes'
import avatorImg from '@/themes/images/avator.gif'

const state = {
  user: '',
  token: utils.getStore('token'),
  name: '',
  nick: '',
  avatar: avatorImg,
  introduction: '',
  visitor: false,
  roles: [],
  perms: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_NICK: (state, nick) => {
    state.nick = nick
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_PERMS: (state, perms) => {
    state.perms = perms
  },
  SET_VISITOR: (state, visitor) => {
    state.visitor = visitor
  }
}

const actions = {
  // user login
  login({ commit }, params) {
    let { userName, password } = params
    userName = userName.trim()
    return new Promise((resolve, reject) => {
      login({
        uname: userName,
        pwd: password
      })
        .then(res => {
          const { code, token, nick, name } = res
          if (code === 0) {
            utils.setStore('token', token)
            commit('SET_TOKEN', token)
            commit('SET_NICK', nick)
            commit('SET_NAME', name)
            resolve(res)
          } else {
            reject(res)
          }
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // get user info
  getUserInfo({ commit }, params) {
    return new Promise((resolve, reject) => {
      getUserInfo(params)
        .then(res => {
          const { code, roles, perms, msg } = res
          if (code === 0) {
            if (!roles || (roles.length === 0 && perms.length === 0)) {
              commit('SET_VISITOR', true)
            } else {
              commit('SET_VISITOR', false)
            }
            commit('SET_ROLES', roles)
            commit('SET_PERMS', perms)
            commit('SET_NICK', res.nick)
            commit('SET_NAME', res.name)
            resolve(res)
          } else {
            reject(msg)
          }
        })
        .catch(err => {
          reject(err)
        })
    })
  },

  // 前端 登出
  frontLogout({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      utils.removeStore('token')
      resolve()
    })
  },

  // user logout
  logout({ commit }, params) {
    return new Promise((resolve, reject) => {
      logout(params)
        .then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMS', '')
          commit('SET_NICK', '')
          commit('SET_NAME', '')
          utils.clearStore()
          resetRouter()
          resolve()
        })
        .catch(err => {
          reject(err)
        })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_ROLES', '')
      commit('SET_USERINFO', {})
      utils.clearStore()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
